<?php 
use lib\{Examine,BaseController};
use lib\protocol\{AliQQ_PayMoney,WxMac,QQPC};
function xufeiqymoneymck($row , $srow){
    $date = date("Y-m-d H:i:s");
    $E_type=['alipay'=>'支付宝免ck'];
    require_once SYSTEM_ROOT  . '/aop/AopClient.php';
    require_once SYSTEM_ROOT .'/aop/request/AlipayDataBillAccountlogQueryRequest.php';
    require_once SYSTEM_ROOT .'/aop/request/AlipayDataBillBalanceQueryRequest.php';	
    $aop = new AopClient ();
    $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
    $aop->appId = $row['alipayAppId'];//应用ID
    $aop->rsaPrivateKey = $row['appPrivateKey'];//私钥
    $aop->alipayrsaPublicKey=$row['alipayPublicKey'];//支付宝公钥
    $aop->apiVersion = '1.0';
    $aop->signType = 'RSA2';
    $aop->postCharset='UTF-8';
    $aop->format='json';
    $request = new AlipayDataBillBalanceQueryRequest ();
    $request->setBizContent("{}");
    $result = $aop->execute($request);
     $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
     $resultCode = $result->$responseNode->code;
      $result = json_decode(json_encode($result), true);
      
      if(is_array($result['alipay_data_bill_balance_query_response'])){
          unset($result['sign']);
          $result1 = $result['alipay_data_bill_balance_query_response'];  
          $money = $result1['available_amount'];
         
          $Pay_Money = $money >= 0.01 ? $money : 0.00;
          Examine::getDB()->update('qrlist',['money'=>$Pay_Money] , ['id'=>$row['id']]);
          $money_ = $row['money'];
         
          $Qr_Money=0;
          if($money_!=$Pay_Money){
              $Qr_Money = $Pay_Money-$money_;//实时余额减去上次系统记录余额=本次收款到账金额
          }
          
          if($Qr_Money>0){
              $Qr_Money1 = round($Qr_Money, 2); 
              $userrow = Examine::getDB()->userrow($row['pid'] , 'pid');
               
              if($srow['status'] == 0 && $Qr_Money1 == $srow['price']){
                  
                  return Examine::getCommon()->get_notify($E_type,$srow,$userrow);
              }
          }
          return false;
      }
      return false;
}



function ALIQQ_money_notify($row,$srow){
    if ($row['type'] == 'alipay'){
        $methodName = ($row['xf_type'] == '1') ? 'Get_pay_money' : 'Get_pay_money' . $row['xf_type'];
        $money = AliQQ_PayMoney::$methodName($row['type'], $row['cookie']);
        
    }else {
        $money = AliQQ_PayMoney::Get_pay_money($row['type'], $row['cookie']);
    }
    
    $E_type = array(
        'qqpay'=>'QQ钱包',
        'alipay'=>'支付宝'
    );
    if(empty($money['status'])){
        return false;
    }
    $Pay_Money = $money['money']>=0.01 ? $money['money'] : 0.00;
    Examine::getDB()->update('qrlist',['money'=>$Pay_Money] , ['id'=>$row['id']]);
    $money_ = $row['money'];
    $Qr_Money= $money_!=$Pay_Money ? $Pay_Money - $money_ : 0;
    if($Qr_Money>0){
        $Qr_Money1 = round($Qr_Money, 2); 
        if($srow['status'] == 0 && $Qr_Money1 == $srow['price']){
             $userrow = Examine::getDB()->userrow($row['pid'] , 'pid');
              return Examine::getCommon()->get_notify($E_type,$srow,$userrow);
        }
    }
	return false;
}

function WxYun_Notidfy($qr_row, $srow) {
    $E_type = [
        'wxpay' => "微信"
    ];

    $WxApi = Examine::Wx_protocol($qr_row);
    if (empty($WxApi)) return false;

    $WxpayMoney = '';
    $Order = $WxApi->Order($qr_row['cookie']);
    
    // 根据 xf_type 选择正则表达式
    $WxNotify = '';
    switch ($qr_row['xf_type']) {
        case '10':
            $WxNotify = '/收款(.*?)元/';
            break;
        case '8':
            $WxNotify = '/二维码赞赏到账(.*?)元/';
            break;
        default:
            $WxNotify = '/支付收款(.*?)元/';
            break;
    }

    preg_match($WxNotify, $Order, $trStrmgm);
    $WxpayMoney = $trStrmgm[1] ?? '';

    if (!empty($WxpayMoney) && $srow['status'] == 0 && $WxpayMoney == $srow['price']) {
        $userrow = Examine::getDB()->userrow($row['pid'], 'pid');
        return Examine::getCommon()->get_notify($E_type, $srow, $userrow);
    }

    if (empty($srow['pay_url'])) {
        return handlePayUrl($qr_row, $srow, $WxApi);
    }
}
function check_usdt($qr_row , $srow){
    
    $money = Examine::getCommon()->CurlUrl->usdtNotify($qr_row['qr_url']);
    if(!$money)return false;
    $Pay_Money = $money >= 0.01 ? $money : 0.00;
    $this->db->update('qrlist',['money'=>$Pay_Money] , ['id'=>$qr_row['id']]);
    $Qr_Money = 0;
    if($qr_row['money'] != $Pay_Money){
        $Qr_Money = $Pay_Money - $qr_row['money'];
    }
    if($Qr_Money>0){
        $Qr_Money1 = round($Qr_Money, 2); 
        $E_type = ['usdt'=>"USDT支付"];
        if($srow['status'] == 0 && $Qr_Money1 == $srow['price']){
            $userrow = Examine::getDB()->userrow($row['pid'], 'pid');
            return Examine::getCommon()->get_notify($E_type, $srow, $userrow);
        }
    }
    return false;
}
function handlePayUrl($qr_row, $srow, $WxApi) {
    switch ($qr_row['xf_type']) {
        case '1':
        case '11':
            return handleIndividualCode($qr_row, $srow, $WxApi);
        case '9':
        case '12':
            return handleBusinessCode($qr_row, $srow, $WxApi);
        case '13':
            return handleBusinessLink($qr_row, $srow, $WxApi);
        case '8':
            return handleAppreciationCode($qr_row, $srow);
        case '10':
            return handleMicroMerchant($qr_row, $srow);
        default:
            return null;
    }
}

function handleIndividualCode($qr_row, $srow, $WxApi) {
    $qr_url = $WxApi->WXF2FQrcode($qr_row['cookie'], $srow['price'], $srow['trade_no']);
    $qr_url = json_decode($qr_url, true)['pay_url'] ?? null;
    if (!empty($qr_url)) {
        Examine::getDB()->update('order', ['pay_url' => $qr_url], ['id' => $srow['id']]);
        return $qr_url;
    }
    return null;
}

function handleBusinessCode($qr_row, $srow, $WxApi) {
    if ($qr_row['xf_type'] == 12) {
        $qr_url = $WxApi->WXJYQrcode($qr_row['cookie'], $srow['price'], $srow['trade_no'], $qr_row['beizhu']);
        $qr_url = json_decode($qr_url, true);
        if ($qr_url['Code'] == 0 && $qr_url['Data']) {
            $qr_url = $qr_url['Data'];
            if (!empty($qr_url)) {
                Examine::getDB()->update('order', ['pay_url' => $qr_url], ['id' => $srow['id']]);
                return $qr_url;
            }
        }
    } else {
        if (empty($qr_row['qr_url'])) return null;
        Examine::getDB()->update('order', ['pay_url' => $qr_row['qr_url']], ['id' => $srow['id']]);
        return $qr_row['qr_url'];
    }
    return null;
}

function handleBusinessLink($qr_row, $srow, $WxApi) {
    $qr_url = $WxApi->WXJYQrcodeUrl($qr_row['cookie'], $srow['price'], $srow['trade_no'], $qr_row['beizhu']);
    $qr_url = json_decode($qr_url, true);
    if ($qr_url['Code'] == 0 && $qr_url['Data']['qrcode']) {
        $qr_url = $qr_url['Data']['qrcode'];
        if (!empty($qr_url)) {
            Examine::getDB()->update('order', ['pay_url' => $qr_url], ['id' => $srow['id']]);
            return $qr_url;
        }
    }
    return null;
}

function handleAppreciationCode($qr_row, $srow) {
    if (empty($qr_row['qr_url'])) return null;
    Examine::getDB()->update('order', ['pay_url' => $qr_row['qr_url']], ['id' => $srow['id']]);
    return $qr_row['qr_url'];
}

function handleMicroMerchant($qr_row, $srow) {
    if (empty($qr_row['qr_url'])) return null;
    Examine::getDB()->update('order', ['pay_url' => $qr_row['qr_url']], ['id' => $srow['id']]);
    return $qr_row['qr_url'];
}


?>